from PIL import Image
import sys
import os
import string

def merge(image1, image2, filename):
  '''Takes two .jpg pictures and a string filename and returns a stitched together picture'''
  im = Image.open(image1)
  im2 = Image.open(image2)
  width1,height1 = im.size
  width2,height2 = im2.size

  if width1 >= width2:
    widthfinal = width1
  else:
    widthfinal = widthfina2
  heightfinal = height1 + height2

  finalim = Image.new('RGB', ((widthfinal,heightfinal)))
  c1=im.crop((0,0,im.size[0],im.size[1]))
  c2=im2.crop((0,0,im2.size[0],im2.size[1]))
  finalim.paste(c1,(0,0,im.size[0],im.size[1]));
  finalim.paste(c2,(0,height1,im2.size[0],heightfinal));

  finalim.save(filename, format = im.format)

  return

if __name__ == '__main__':
	print sys.argv[1:]
	filepath=sys.argv[1]
	if not os.path.exists(filepath):
		sys.exit(1);
	filepath=string.replace(filepath,'.swf','');
	if os.path.exists(filepath+'.jpg'):
		sys.exit(1);
	i=1;
	while os.path.exists(filepath+'-%s.jpg'%i):
		if i==1:
			merge(filepath+'-1.jpg',filepath+'-2.jpg',filepath+".jpg")
			i=3
		else:
			merge(filepath+'.jpg',filepath+'-%s.jpg'%i,filepath+".jpg")
			i=i+1